/* 	CalendarField
*	Nice calendar navigation
*	
*	IMPORTANT
*
*	
*										*/
var CalendarField = function( ref, set )	{

	Object.extend( obj = $$( '.CalendarField' ).pop(), CalendarField.proto );
	Object.extend( obj, CalendarField[ "proto_" + ref.react ]);
	obj.ref = ref;
	
	obj.select( ".header" )[0].insert(
		new Element( "div", { "class":"arrow left" }).observe( 'click', obj.load.bind( obj ))
	).insert(
		new Element( "div", { "class":"month" }).observe( 'click', obj.load.bind( obj )).update( "..." )
	).insert(
		new Element( "div", { "class":"arrow right" }).observe( 'click', obj.load.bind( obj ))
	);
	
	obj.init();
	if( set ) obj.seed( set );
	
	return obj;
}


CalendarField.proto = {
	
	current		: 0,
	
	load		: function( el ) {
		
		this.current += ( el.currentTarget.hasClassName( "left" ) )? -1 : 1;
		this.select( ".month" )[0].update( "..." );
		
		xhttp.send( '/xhttp.mod', { mod: "calendar.field", action: "mini", vars: { current: this.current }}, this.pull_response.bind( this ));
	},
	
	seed		: function( set ) {
		this.set = set;
		this.count = set.first;
		
		this.select( ".month" )[0].update( set.month );
		this.select( "span.node" ).each( this.fillnode.bind( this ));
		this.checkday();
		
		if( this.multi ) this.multi.hide();
	},
	
	fillnode	: function( span, n) {
			
			span.className = ( ++this.count <= 0 || this.count > this.set.days ) ? 'node blur': 'node';
			span.update(
				( this.count > 0 )? (( this.count > this.set.days )? this.count - this.set.days : this.count ) : this.set.prevdays + this.count
			).stopObserving().n = this.count;
			
			if( this.set.nodes && this.set.numref[ n ] )
				span.observe( "mouseover", this.hover ).observe( "click", this.bubble ).addClassName( "active" ).ref = this.set.numref[ n ];
	},
	
	checkday	: function() {
		
		switch ( this.current ) { 
			case -1	: num = this.set.today + this.set.days - this.set.first; break;
			case 0	: num = this.set.today - this.set.first; break;
			case 1	: num = this.set.today - this.set.prevdays - this.set.first; break;
			default	: num = -1;
		}
		if( num > 0 && num < 42 ) this.select( "span.node" )[ num-1 ].addClassName( "today" );
	},
	
	goto		: function( id ) {
		window.location = "/agenda/dag?calid=" + id;
	},
	
	pull_response	: function( response ) {
		
		set = response.responseText.evalJSON();
		this.seed( set );
	},
	
	bubble		: function() {}

}

CalendarField.proto_info = {
	
	init		: function() {
	
		this.insert( { top: new Element( "div", { "class":"info" }).update(
			new Element( "h1" ).update( "Klik dag voor detailpagina" )
		) });
	},
	
	hover		: function() {
		obj = this.up(1);
		descr = this.innerHTML + "." + (( this.n < 0 )? obj.set.prevstr : ( this.n <= obj.set.days )? obj.set.curstr: obj.set.nextstr ); 
		
		info = obj.select( ".info" )[0].update(
			new Element( "h1" ).update( descr )
		).insert( ul = new Element( "ul" ));
		
		this.ref.each( function( id ){
			ul.insert( "<li class=''><strong>" + obj.set.nodes[ id ].name + "</strong>" + obj.set.nodes[ id ].hour + ", " + obj.set.locations[ obj.set.nodes[ id ].location ] + "</li>" );
		});
	},
	
	bubble		: function() {
		
		this.up(1).select( ".info" )[0].update( "daar gaan we..." );
		obj.goto( this.ref[0] );
	}
}

CalendarField.proto_multi = {
	
	init		: function() {
	
		this.insert( this.multi = new Element( "ul", { "class": "multi" }).hide() );
	},
	
	hover		: function() {
		multi = this.up(1).multi.clonePosition( this, { setWidth:false, setHeight:false, offsetTop:-11 }).update().show();
		
		this.ref.each( function( id ){
			
			multi.insert( "<li class=''><a href='" + this.ref.uri + "?calid=" + id + "'>" + this.set.nodes[ id ].name + "</a> " + this.set.nodes[ id ].hour + "<br /><a href='" + this.set.nodes[ id ].uri + "' class='bio'>bio</a> " + this.set.locations[ this.set.nodes[ id ].location ] + "</li>" );
		}.bind( this.up(1) ));
	}
}














/*calendar = {
			cur		: 0,
			multi	: 0,
			load	: function(n) {
				$('refcal.month').update("dum dum...");
				calendar.cur += n;
				if(this.multi) $('float').hide();
				xhttp.send("/inc/ajx/mod.php", {wtype:'ajax', mod:'group', vars:{ 'n':calendar.cur, 'multi':this.multi }});
			},
			parse	: function(count) {
				$R(1, 42).each(function(n) { count++;
					$('refcal.'+n).className = (count<=0 || count > calendar.totalm) ? 'blur':'';
					$('refcal.'+n).stopObserving();
					if(calendar.dates['refcal.'+n]) {
						$('refcal.'+n).addClassName('active');
						$('refcal.'+n).observe('mouseover',calendar.blub);
					}
  					input = (count>0)? ((count > calendar.totalm)? count - calendar.totalm : count) : calendar.tprev + count;
  					$('refcal.'+n).update(input);
				});
			},
			blub 	: function() {
				
				if(calendar.multi) {
					vars = calendar.dates[this.id]; name='';
					$('float').clonePosition(this, {setWidth:0,setHeight:0,offsetLeft:0,offsetTop:0});
					$('float').update();
					vars.each(function(o){ $('float').appendChild(new Element('a',{href:"/artists/bio?id="+o.ID,"style":(o.otype==2)?"color:#aaa;":""}).update(o.name+"<br />")); });
					//$('float').update(name);
					$('float').show();
				} else {
					vars = calendar.dates[this.id][0];
					$('hint').clonePosition(this, {setWidth:0,setHeight:0,offsetLeft:6,offsetTop:21});
					$('hint.cont').update(vars.name);
					$('hint').show();
					this.observe('click', calendar.doinfo);
					this.observe('mouseout',calendar.unblub);
				}
			},
			refer	: function() {
					$('hint').clonePosition(this, {setWidth:0,setHeight:0,offsetLeft:6,offsetTop:21});
					$('hint.cont').update("klik voor volledig overzicht");
					$('hint').show();
					this.observe('click', function(){ window.location = "/agenda/cal.alles";});
					this.observe('mouseout',calendar.unblub);
			},
			unblub	: function() { $('hint').hide(); },
			closest	: function(now) {
				$('refcal.'+now).addClassName('now');
				for(n = now; n<42; n++) {
					if(calendar.dates['refcal.'+n]) { calendar.doinfo('refcal.'+n); break; }
				}
			},
			doinfo	: function(e) {
				vars = calendar.dates[(typeof e == 'string')? $(e).id : e.target.id][0];
				$('refcal.info.title').update();
				$('refcal.info.title').appendChild( new Element('a', {href:'/artiesten/bio?id='+vars.ID}).update(vars.name));
				$('refcal.info.date').update(vars.date);
				$('refcal.info.content').update(vars.content);
				$('refcal.info.content').appendChild(new Element('a', {href:'/artiesten/bio?id='+vars.ID}).update(' ...meer'));
				if(vars.alt) $('refcal.info.content').innerHTML += "<br />"+vars.alt;
			},
			goto	: function() {
				window.location = "/artists/bio?id="+calendar.dates[this.id][0].ID;
			}
		}
*/